package fileoIO;

import java.io.File;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入目标目录:");
        String fileName = scanner.next();
        File file = new File(fileName);
        if(!file.isDirectory()){
            System.out.println("此目录不存在!");
            return ;
        }
        System.out.println("请输入关键字:");
        String keyWord = scanner.next();
        srcFile(file,keyWord);
    }

    private static void srcFile(File file, String keyWord) {
        File[] files = file.listFiles();
        for(File file1 : files){
            if (file1.isDirectory()) {
                srcFile(file1,keyWord);
            }else {
                dealFile(file1,keyWord);
            }
        }
    }

    private static void dealFile(File file1, String keyWord) {
        Scanner scanner = new Scanner(System.in);
        if(file1.getName().contains(keyWord)){
            System.out.println(file1.getAbsolutePath()+" 是否要删除(n/y)");
            String ans = scanner.next();
            if(ans.equals("y")){
                file1.delete();
            }
        }
    }

}






